#!/usr/bin/env python

import optparse
import sqlite3

conn = sqlite3.connect('/etc/webstatus/domains.db', isolation_level='exclusive')
c = conn.cursor()

def insert(website):
	"""Inserts the url record into the database"""
	command = "INSERT INTO domains VALUES('%s')" % website
	c.execute(command)
	conn.commit()
	c.close()

def delete(website):
	"""Deletes the url record from the databse"""
	command = "DELETE FROM domains where name = ('%s')" % website
	c.execute(command)
	conn.commit()
	c.close()

def query():
	""" Lists the domains in the database """
	command = "SELECT * FROM domains"
	return c.execute(command)

def main():
	p = optparse.OptionParser()
	p.add_option('--insert', '-i', help="insert a value")
	p.add_option('--delete', '-d', help="delete a value")
	p.add_option('--query', '-q', action="store_true",
				dest="query", help="query the domains table")
	options, args = p.parse_args()

	if options.insert:
		insert(options.insert)
	if options.delete:
		delete(options.delete)
	if options.query:
		for i in query():
			print i

if __name__ == '__main__':
	main()
			
